<?php
namespace app\index\controller;
 
 //导入Controller
 use think\Controller;
 //导入数据库
 use think\Db;
//开启session_start
session_start();
class Cart extends Allow{
    //加入购物车
    public function getaddcart(){
      //请求对象
      $request=request();
      $id=$request->param("id");
      $gnum=$request->param("gnum");
      //获取商品数据
      $shop=Db::table("pro_goods")->where("id","{$id}")->select();
      if(isset($_SESSION['cart'][$id])){
        //获取当前商品信息
         $info=Db::table("pro_goods")->where("id","{$id}")->find();
         if($_SESSION['cart'][$id]['num']+$gnum<$info['num']){
          //数量加1
          $_SESSION['cart'][$id]['num']+=$gnum;
         }else{
          //把库存赋值给变量
          $_SESSION['cart'][$id]['num']=$info['num'];
         }

      }else{
        //初始化数量为1
        $shop[0]['num']=$gnum;
        //存储在session里
        $_SESSION['cart'][$id]=$shop[0];
        // var_dump( $_SESSION['cart']);
      }
      $this->success("加入购物车成功","/cart/index");
    }

    //购物车页面
    public function getindex(){
      if(isset($_SESSION['cart'])){
      //加载购物车模版
      //品牌遍历
      $brand=Db::table("brand")->select();
     return $this->fetch("Cart/cart",['cart'=>$_SESSION['cart'],'brand'=>$brand]);
    }else{
      $this->error("未选中购物车内商品","/home/index");
    }
}
    //数量减
    public function getjian(){
       //请求对象
        $request=request();
        //获取参数id
        $id=$request->param("id");
        //让当数量前商品减一
        $_SESSION['cart'][$id]['num']-=1;
         if($_SESSION['cart'][$id]['num']<1){
            $_SESSION['cart'][$id]['num']=1;
        }
        //数组
         $data['num']=$_SESSION['cart'][$id]['num'];
        //总计
        $data['tot']=$_SESSION['cart'][$id]['num']*$_SESSION['cart'][$id]['price'];
        echo json_encode($data);
    }

    //数量加
    public function getjia(){
        $request=request();
        //获取参数id
        $id=$request->param("id");
        //数量加1
        $_SESSION['cart'][$id]['num']+=1;
        //获取最大库存
        $info=Db::table("pro_goods")->where("id","{$id}")->find();
        if($_SESSION['cart'][$id]['num']>$info['num']){
            $_SESSION['cart'][$id]['num']=$info['num'];
        }
        //数量和总计
        $data['num']=$_SESSION['cart'][$id]['num'];
        $data['tot']=$_SESSION['cart'][$id]['num']*$_SESSION['cart'][$id]['price'];
        echo json_encode($data);
    }

    //删除
    public function getdel(){
      $request=request();
        //获取id
      $id=$request->param("id");
        // echo $id;
      unset($_SESSION['cart'][$id]);
      if(empty($_SESSION['cart'])){
        echo 2;
      }else{
        echo 1;
      }
  }

  //选中删除
  public function getdels(){
      //获取arr参数
      $arr=isset($_GET['arr'])?$_GET['arr']:'';
      if($arr==""){
        echo "请至少选中一个商品";
        exit;
      }

      //遍历
      foreach($arr as $k=>$v){
        unset($_SESSION['cart'][$v]);
      }
       if(empty($_SESSION['cart'])){
        echo 2;
      }else{
        echo 1;
      }
  }

  //选中总价
  public function getxuan(){
        $a=0;
        $b=0;
    if(isset($_GET['arr'])){
      $arr = $_GET['arr'];
      foreach($arr as $k=>$v){
      $a+=$_SESSION['cart'][$v]['num']; 
      $b+=$_SESSION['cart'][$v]['price']*$_SESSION['cart'][$v]['num'];
      }
      $data['gnum']=$a;
      $data['allprice']=$b;
      echo json_encode($data);
      }

    else{
      echo 0;
        }
   
  }
  //全选相加
  public function getquanjia(){
        $a=0;
        $b=0;
      if(isset($_GET['arr2'])){
        $arr2 = $_GET['arr2'];
         foreach($arr2 as $k=>$v){
          $a+=$_SESSION['cart'][$v]['num']; 
          $b+=$_SESSION['cart'][$v]['price']*$_SESSION['cart'][$v]['num'];
         }
          $data['gnum1']=$a;
          $data['allprice1']=$b;
          echo json_encode($data);
      }
  }

  //反选相加
  public function getfanjia(){
        $a=0;
        $b=0;
      if(isset($_GET['arr3'])){
        $arr3 = $_GET['arr3'];
         foreach($arr3 as $k=>$v){
          $a+=$_SESSION['cart'][$v]['num']; 
          $b+=$_SESSION['cart'][$v]['price']*$_SESSION['cart'][$v]['num'];
         }
          $data['gnum2']=$a;
          $data['allprice2']=$b;
          echo json_encode($data);
      }else{
        echo 0;
      }
  }

  //创建订单session
  public function getorder(){
     if(isset($_GET['arr1'])){
        $arr1= $_GET['arr1'];
        unset($_SESSION["order"]);
        foreach($arr1 as $k=>$v){

          $_SESSION['order'][$v]="";
          $_SESSION['order'][$v]=$_SESSION['cart'][$v];
        }
        // print_r($arr1);
        echo 1;
     }else{
      echo 0;
     }
  }

   //选中+-查询总量
   public function getchaxun(){
      $a=0;
      $b=0;
    if(isset($_GET['arr5'])){
      $arr5 = $_GET['arr5'];
      foreach($arr5 as $k=>$v){
      $a+=$_SESSION['cart'][$v]['num']; 
      $b+=$_SESSION['cart'][$v]['price']*$_SESSION['cart'][$v]['num'];
      }
      $data['gnum2']=$a;
      $data['allprice2']=$b;
      echo json_encode($data);
      }

    else{
      echo 0;
        }
   }


   //全选+-查询总量
   public function getquanchaxun(){
      $a=0;
      $b=0;
    if(isset($_GET['arr6'])){
      $arr6 = $_GET['arr6'];
      foreach($arr6 as $k=>$v){
      $a+=$_SESSION['cart'][$v]['num']; 
      $b+=$_SESSION['cart'][$v]['price']*$_SESSION['cart'][$v]['num'];
      }
      $data['gnum3']=$a;
      $data['allprice3']=$b;
      echo json_encode($data);
      }

    else{
      echo 0;
        }
   }

//未选中商品提交
public function geterror(){
  $this->error("未选中购物车内商品","/cart/index");
}

}  
